cmake_minimum_required(VERSION 3.10)
project(soeg_server VERSION 1.0.0 DESCRIPTION "Service Oriented Edge Gateway")

set(CMAKE_CXX_STANDARD 11) # C++11标准
set(CMAKE_CXX_STANDARD_REQUIRED ON) # C++11标准是必须的
set(CMAKE_CXX_EXTENSIONS OFF) # 不使用编译器特定扩展

# =============================== 告警设置 ==========================
set(STRICT_WARNINGS
    -Wall
    -Wextra

    # # 关键：检测类型转换（包括 uint32_t→uint16_t 截断）
    # -Wconversion # 检测所有隐式类型转换（含精度损失）
    # -Wsign-conversion # 检测有符号/无符号类型转换

    # # 可选：更严格的检查
    # -Wpedantic # 严格遵循 C++ 标准
    # -Wshadow # 检测变量遮蔽
    # -Wuninitialized # 检测未初始化变量
    # -Wunused # 检测未使用的变量/函数
)

# 将告警参数应用到所有目标
add_compile_options(${STRICT_WARNINGS})

# =============================== 全局配置 ==========================
set(ROOT ${CMAKE_CURRENT_SOURCE_DIR}) # 项目根目录
set(MYLIB soeg_core) # 静态库名称
set(MYEXE soeg_server) # 可执行程序名称

# 自定义宏(调试模式、启用MOCK、日志颜色)
add_definitions(-DDEBUG -DMOCK -DLOG_COLOR)

# 输出路径(默认为空,可被父CMAK定义覆盖)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${LIBRARY_OUTPUT}) # 静态库输出目录
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${ARCHIVE_OUTPUT}) # 动态库输出目录
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${RUNTIME_LIBRARY}) # 程序文件输出目录

# 资源文件
file(GLOB_RECURSE HEADERS "${ROOT}/include/*.h" "${ROOT}/libs/*.h") # 头文件
file(GLOB_RECURSE SOURCES "${ROOT}/src/*.cpp" "${ROOT}/libs/*.c") # 源文件
list(FILTER SOURCES EXCLUDE REGEX "${ROOT}/src/main.cpp") # 排除文件

# =============================== 生成lib ==========================
add_library(${MYLIB} STATIC ${SOURCES} ${HEADERS}) # 生成静态库
target_include_directories(${MYLIB} PUBLIC ${ROOT}/include ${ROOT}/libs) # 头文件路径

# =============================== 程序项目 ==========================
add_executable(${MYEXE} ${SOURCES} ${HEADERS} ${ROOT}/src/main.cpp) # 可执行程序
target_include_directories(${MYEXE} PRIVATE ${ROOT}/include) # 头文件路径(仅main相关)
target_link_libraries(${MYEXE} PRIVATE ${MYLIB}) # 依赖项目库

file(MAKE_DIRECTORY ${CMAKE_BINARY_DIR}/logs) # 创建日志目录
file(COPY ${CMAKE_SOURCE_DIR}/etc DESTINATION ${CMAKE_BINARY_DIR}) # 拷贝配置文件

# =============================== 条件编译 ==========================
if(WIN32)
    target_link_libraries(${MYLIB} PUBLIC ws2_32) # WinSock2.0_32核心库

    # target_link_directories(${MYLIB} PUBLIC "C:/libs") # 静态库路径(可多个)
endif()

if(MSVC) # MSVC无Wextra编译参数; 无须链接pthread库
    target_compile_options(${MYLIB} PUBLIC -g -Wall "/W4") # 调试信息+所有警告+警告级别
    set_property(GLOBAL PROPERTY USE_FOLDERS ON) # 启用VS工程文件组
    set_property(DIRECTORY ${ROOT} PROPERTY VS_STARTUP_PROJECT ${MYEXE}) # 默认启动项
    add_definitions(-D_CRT_SECURE_NO_WARNINGS -DWIN32 -D_WIN32) # VS宏定义
    source_group(TREE ${ROOT} FILES ${SOURCES} ${HEADERS}) # VS目录分组

    # 设置utf8编码
    # target_compile_options(${MYLIB} PRIVATE /utf-8)
    add_compile_options("$<$<C_COMPILER_ID:MSVC>:/utf-8>")
    add_compile_options("$<$<CXX_COMPILER_ID:MSVC>:/utf-8>")
else()
    target_compile_options(${MYLIB} PUBLIC -g -Wall -Wextra) # 调试信息+所有警告+额外警告
    target_link_libraries(${MYLIB} PUBLIC pthread) # 静态库链接

    # target_link_directories(${MYLIB} PUBLIC ${HOME}/local/libs) # 静态库路径(可多个)
endif()

# =============================== 测试项目 ==========================
option(BUILD_TEST "Build test projects" ON)

if(BUILD_TEST)
    enable_testing()
    add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/tests)
endif()